//
//  PersonalSettingVC.m
//  sensen_ios
//
//  Created by mc on 16/2/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PersonalSettingVC.h"


#import "PersonalVC.h"

#import "ModifyNickVC.h"
#import "ModifyMobileVC.h"
#import "ModifyEmailVC.h"
#import "WebViewController.h"
#import "WXApi.h"
#import "SpecialStateVC.h"
@interface PersonalSettingVC ()<UIActionSheetDelegate,WXApiDelegate>
{
    UITableView *_tableView; // 表视图，覆盖整个界面
    NSArray *_cellNameArray; // 存储单元格内容的数组
    UIImageView *_headerView; // 用户头像
    UILabel *_emailLabel; // 邮箱显示
    UILabel *_phoneLable; // 手机显示
    UILabel *_nickName;
    UILabel *_serviewPhoneLable; //客服电话显示
}
@end

@implementation PersonalSettingVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人设置";
    // 创建导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];
    // 设置单元格内容数组
    NSArray *section1 = @[@"头像"];
    NSArray *section2 = @[@"昵称"];
    NSArray *section3 = @[@"手机",@"邮箱",@"微信"];
    NSArray *section4 = @[@"当前版本",@"关于森森",@"使用条款与协议",@"特别说明",@"联系客服"];
    _cellNameArray = @[section1,section2,section3,section4];
    
    // 创建表视图tableView
    [self _createTableView];
    
    
    //监听微信登陆
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getWeChatBindCode:) name:@"weixinBind" object:nil];

    
    
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    // 进来的时候要隐藏导航栏
    MainTabBarController *tabBarController = (MainTabBarController *)self.navigationController.tabBarController;
    [tabBarController hideTabBar];

    // 设置导航栏自定义样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    // 更新邮箱
    UserModel *user = [CommonCache getUserInfo];
    if (user.email && (_emailLabel.text != user.email) && _emailLabel) {
        _emailLabel.text = user.email;
    }
    
    //if (![user.reg_from isEqualToString:@"2"] ) {
        if (user.nickname.length > 0) {
            _nickName.text = user.nickname;
            
        }else{
            NSMutableString *name = [NSMutableString stringWithString:user.username];
            [name replaceCharactersInRange:NSMakeRange(4, 5) withString:@"****"];
            _nickName.text = name;
        }
        
        
//    }else{
//        _nickName.text = user.nickname;
//    }

    
    
    if (user.mobile && _phoneLable.text != user.mobile) {
        NSMutableString *name = [NSMutableString stringWithString:user.mobile];
        [name replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        _phoneLable.text = name;
    }
    
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter]  removeObserver:self name:@"weixinLogin" object:nil];
}


#pragma mark - 创建子视图
// 创建表视图tableView
- (void)_createTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    _tableView.separatorInset = UIEdgeInsetsZero;
//    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//        _tableView.layoutMargins = UIEdgeInsetsZero;
//        _tableView.contentInset = UIEdgeInsetsMake(-40, 0, 0, 0);
//    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = rgba(237, 238, 239, 1);
    
    
}

#pragma mark - 退出登录
- (void)logOutAction:(UIButton *)button{
    UserModel *user = [CommonCache getUserInfo];
    if (user == nil) {
        [SVProgressHUD showOnlyStatus:@"您没有登录" withDuration:1.5];
        return;
    }
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        
        // 创建警告弹窗
        NSString *alertTitle = @"确定要退出登录？";
        NSString *yesButtonTitle = @"确定";
        NSString *cancelButtonTitle = @"取消";
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        // 添加点击动作
        // 取消
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
        // 确定
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:yesButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
                                    {
                                        //直接退出
                                        [self _userLogout];
                                    }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:yesAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
    else{
        //直接退出
        [self _userLogout];
        
    }

}


// 用户退出账户
- (void)_userLogout
{
    // 清理本地账户信息
    [CommonCache logout];
    _headerView.image = [UIImage imageNamed:@"默认头像"];
    [SVProgressHUD showOnlyStatus:@"已退出登录！" withDuration:1.5];
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - tableView代理方法
// 单元格组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _cellNameArray.count;
}

// 每组的单元格数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = _cellNameArray[section];
    return array.count;
}


// 创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
//    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//        cell.layoutMargins = UIEdgeInsetsZero;
//    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 添加单元格文字
    cell.textLabel.text = _cellNameArray[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    
    // 单元格右端图标
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 头像
    if (indexPath.section == 0) {
        _headerView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 5, 30, 30)];
        _headerView.tag = 123;
        _headerView.backgroundColor = [UIColor lightGrayColor];
        _headerView.layer.cornerRadius = 15;
        _headerView.layer.masksToBounds = YES;
        // 设置头像
        UserModel *user = [CommonCache getUserInfo];
        if (user != nil && ![user.head isStringNull]) {
            [_headerView setImageWithURL:[NSURL URLWithString:user.head]];
        } else {
            _headerView.image = [UIImage imageNamed:@"默认头像"];
        }
        [cell addSubview:_headerView];
    }
    
    // 昵称
    else if (indexPath.section == 1 && indexPath.row == 0) {
        _nickName = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-130, 0, 100, cell.height)];
        _nickName.textAlignment = NSTextAlignmentRight;
        _nickName.font = [UIFont systemFontOfSize:15];
        _nickName.textColor = [UIColor darkGrayColor];
        // 昵称
        UserModel *user = [CommonCache getUserInfo];
        if (user) {
            // 如果是微信登录，则显示昵称
            if (![user.reg_from isEqualToString:@"2"] ) {
                if (user.nickname.length > 0) {
                    _nickName.text = user.nickname;
                    
                }else{
                    NSMutableString *name = [NSMutableString stringWithString:user.username];
                    [name replaceCharactersInRange:NSMakeRange(4, 5) withString:@"****"];
                    _nickName.text = name;
                }
        
               
            }
            else {
                _nickName.text = user.nickname;
                
            }
        }
        [cell addSubview:_nickName];
        }
    
    // 手机号
    else if (indexPath.section == 2 && indexPath.row == 0) {
        _phoneLable = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH-80, cell.height)];
        _phoneLable.textAlignment = NSTextAlignmentRight;
        _phoneLable.font = [UIFont systemFontOfSize:15];
        _phoneLable.textColor = [UIColor darkGrayColor];
        UserModel *user = [CommonCache getUserInfo];
        NSLog(@"手机号码  %@", user.mobile);
        if (user.mobile.length > 0) {
            NSMutableString *name = [NSMutableString stringWithString:user.mobile];
            [name replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
            _phoneLable.text = name;
        }
        [cell addSubview:_phoneLable];
    }
    
    //显示电子邮箱
    else if (indexPath.section == 2 && indexPath.row == 1) {
        
        _emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH-80, cell.height)];
        _emailLabel.textAlignment = NSTextAlignmentRight;
        _emailLabel.font = [UIFont systemFontOfSize:15];
        _emailLabel.textColor = [UIColor darkGrayColor];
        UserModel *user = [CommonCache getUserInfo];
        if (user.email && (_emailLabel.text != user.email)) {
            _emailLabel.text = user.email;
        }
        [cell addSubview:_emailLabel];
    }
    
    
    else if (indexPath.section == 2 && indexPath.row == 2){
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH-70, cell.height)];
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = SS_NAVI_BLUE_COLOR;
        label.tag = 99999;
        [cell addSubview:label];
        
        UserModel *user = [CommonCache getUserInfo];
        NSLog(@"wxopenid = %@", user.wxopenid);
        NSLog(@"reg_from =%@", user.reg_from);
        if (user.wxopenid.length > 0) {
            label.text = @"已绑定";
        }else{
            label.text = @"未绑定";
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    }

    
    //显示当前版本号
    else if (indexPath.section == 3 && indexPath.row == 0) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, SCREEN_WIDTH-70, cell.height)];
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor darkGrayColor];
        label.text = kCurrentVersion;
        [cell addSubview:label];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    
    
    //显示联系客服
    else if (indexPath.section == 3 && indexPath.row == 4) {
        _serviewPhoneLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -120, 0, 120, cell.height)];
        _serviewPhoneLable.textAlignment = NSTextAlignmentLeft;
        _serviewPhoneLable.font = [UIFont systemFontOfSize:15];
        _serviewPhoneLable.textColor = [UIColor darkGrayColor];
        _serviewPhoneLable.text = @"0580-8253177";
        [cell addSubview:_serviewPhoneLable];
        
        UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_serviewPhoneLable addGestureRecognizer:tap1];
        _serviewPhoneLable.userInteractionEnabled = YES;
       
        
        UILabel *serviewPhoneLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -250, 0, 120, cell.height)];
        serviewPhoneLable.textAlignment = NSTextAlignmentRight;
        serviewPhoneLable.font = [UIFont systemFontOfSize:15];
        serviewPhoneLable.textColor = [UIColor darkGrayColor];
        serviewPhoneLable.text = @"0580-8253155";
        [cell addSubview:serviewPhoneLable];
        serviewPhoneLable.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction2:)];
        [serviewPhoneLable addGestureRecognizer:tap2];


        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    
    return cell;
}


- (void)tapAction:(UITapGestureRecognizer *)tap {
    
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:0580-8253177"];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];

}

- (void)tapAction2:(UITapGestureRecognizer *)tap {

    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:0580-8253155"];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];

}


// 组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }else if (section == 1){
        return 6;
    }else{
        return 20;
    }
}

// 组尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
   
    if (section == 3) {
        return 100;
    }else{
        return 5;
    }
}



-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 3) {
        
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 100)];
        footerView.backgroundColor = rgba(237, 238, 239, 1);
        
        // 创建退出登录按钮
        UIButton  * LogOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [footerView addSubview:LogOutBtn];
        [LogOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [LogOutBtn addTarget:self action:@selector(logOutAction:) forControlEvents:UIControlEventTouchUpInside];
        [LogOutBtn setBackgroundColor:SS_NAVI_BLUE_COLOR];
        
        
        [LogOutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footerView).offset(50);
            make.centerX.equalTo(footerView.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 0.8, SCREEN_WIDTH / 8));
            
        }];
        LogOutBtn.layer.borderColor = SS_NAVI_BLUE_COLOR.CGColor;
        LogOutBtn.layer.borderWidth = 1;
        LogOutBtn.layer.masksToBounds = YES;
        LogOutBtn.layer.cornerRadius = 3.0;
               
        return footerView;

    }else{
        return nil;
    }
}


// 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

// 单元格点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 第0组，上传头像
    if (indexPath.section == 0) {
        //先判断是否是登录状态
        if ([CommonCache getUserInfo] == nil) {
            [SVProgressHUD showOnlyStatus:@"您还未登录！" withDuration:1];
            return;
        }
        
        // 适配版本iOS8.0以上
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
            // 创建ActionSheet
            NSString *albumButtonTitle = @"从相册中选择";
            NSString *cameraButtonTitle = @"拍照";
            NSString *cancelButtonTitle = @"取消";
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            
            // 添加点击动作
            // 取消
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
            // 相册中
            UIAlertAction *albumAction = [UIAlertAction actionWithTitle:albumButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
                                          {
                                              [self _choosePhotoFromAlbum];
                                          }];
            // 拍照
            UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:cameraButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
                                           {
                                               [self _getphotoFromCamera];
                                           }];
            
            // Add the actions.
            [alertController addAction:cancelAction];
            [alertController addAction:albumAction];
            [alertController addAction:cameraAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
        // 适配版本8.0以下
        else {
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选择",@"拍照", nil];
            [sheet showInView:self.view];
        }
    }
    
    
    // 第1组
    else if (indexPath.section == 1) {
        
             //如果是手机号登录，密码可以修改
//            UserModel *user = [CommonCache getUserInfo];
//            if (![user.reg_from isEqualToString:@"2"]) {
                ModifyNickVC *vc = [[ModifyNickVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
//            }
//            else {
//                [SVProgressHUD showOnlyStatus:@"微信登录时此项不可修改！" withDuration:1.5];
//            }
        
    }
    
    // 第2组
    else if (indexPath.section == 2) {
        
        // 电子邮箱
        if (indexPath.row == 1) {
            ModifyEmailVC *vc = [[ModifyEmailVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        // 手机
        if (indexPath.row == 0) {
            ModifyMobileVC *vc = [[ModifyMobileVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        // 微信
        if (indexPath.row == 2) {
            UserModel *user = [CommonCache getUserInfo];
            if (user.wxopenid.length > 0 || [user.reg_from isEqualToString:@"2"]) {
                
                [SVProgressHUD showOnlyStatus:@"已经绑定微信" withDuration:1];
                
            }else{
                 [self wxLogin];
            }

        }
        
    }
    
    else if (indexPath.section == 3) {//第三组
    
        if (indexPath.row == 1) {
            WebViewController *vc = [[WebViewController alloc] initWithUrlString:@"http://sunsun.itboye.com/Public/html/about.html"];
            vc.titleString = @"关于森森";
            
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 2){
            
            WebViewController *vc = [[WebViewController alloc] initWithUrlString:@"http://sunsun.itboye.com/Public/html/copyright.html"];
            vc.titleString = @"使用条款与协议";
            [self.navigationController pushViewController:vc animated:YES];

        }else if (indexPath.row == 4){
        
                
            
            
            
        }else if (indexPath.row == 3){
            SpecialStateVC *vc = [[SpecialStateVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
        
    }
}

// 单元格组头视图
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = rgba(28, 30, 31, 1);
    label.font = [UIFont systemFontOfSize:13];
    if (section == 2) {
        label.text = @"    账户设置";
    }
    else if (section == 3) {
        label.text = @"    关于";
    }
    return label;
}




#pragma mark - 头像图片选择相关操作
// 从相册中选择照片
- (void)_choosePhotoFromAlbum
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        [SVProgressHUD showErrorWithStatus:@"相册不可用！" withDuration:1];
        return;
    }
    [self _changeToPickerController:sourceType];
}

// 通过拍照获取照片
- (void)_getphotoFromCamera
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        [SVProgressHUD showErrorWithStatus:@"相机不可用！" withDuration:1];
        return;
    }
    [self _changeToPickerController:sourceType];
}


// 创建并跳转到图片获取界面
- (void)_changeToPickerController:(UIImagePickerControllerSourceType)sourceType
{
    //创建图片拾取界面
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.delegate = self;
    //设置可编辑
    pickerController.allowsEditing = YES;
    pickerController.sourceType = sourceType;
    pickerController.title = @"头像选择";
    [pickerController.navigationBar setTintColor:[UIColor darkGrayColor]];
    [self presentViewController:pickerController animated:YES completion:nil];
}

//UIImagePickerController代理方法，选取图片之后处理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *editedImage = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
    if (editedImage) {
        // 保存到相册
        UIImageWriteToSavedPhotosAlbum(editedImage, nil, nil, nil);
        // 设置成头像
        _headerView.image = editedImage;
    }
    else if (originalImage) {
        // 设置头像
        _headerView.image = originalImage;
    }
    // 返回“个人”界面
    [self dismissViewControllerAnimated:YES completion:^{
        // 上传图片
        [BoyeHttpClient uploadImage:_headerView.image
                               type:@"avatar"
                            success:^(NSDictionary *imageData) {
                                // 头像上传成功，将其网址存储到本地
                                NSString *imageUrl = imageData[@"imgurl"];
                                UserModel *user = [CommonCache getUserInfo];
                                user.head = imageUrl;
                                [CommonCache setUserInfo:user];
                                
                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                NSLog(@"error ==== %@", error);
                                _headerView.image = [UIImage imageNamed:@"默认头像"];
                            }];
    }];
}


#pragma mark - UIActionSheet代理方法（适配iOS8.0以下时使用）
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 选择照片
    // 从相册中选择
    if (buttonIndex == 0) {
        [self _choosePhotoFromAlbum];
    }
    // 通过拍照获取
    else if (buttonIndex == 1) {
        [self _getphotoFromCamera];
    }
}


#pragma mark -- 微信登陆相关 --
-(void) onReq:(BaseReq*)req{
    DLog(@"onReq");
}

/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容，是自动释放的
 */
-(void) onResp:(BaseResp*)resp{
    DLog(@"onResp");
}

// 微信登陆点击事件，在此点击后appDelegate中就会发送通知，然后再这里接收到通知获得code
- (void)wxLogin{
    [self _sendAuthRequest];
}

-(void)_sendAuthRequest
{
    //构造SendAuthReq结构体
    SendAuthReq* req =[[SendAuthReq alloc ] init ];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"ss_weixin" ;
    
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendAuthReq:req viewController:self delegate:self];
    
}

// 微信登陆通知方法
-(void)getWeChatBindCode:(NSNotification *)notification{
    
    NSString *code = [[notification userInfo] objectForKey:@"code"];
    NSString *state = [[notification userInfo] objectForKey:@"state"];
    
    if (![state isEqualToString:@"ss_weixin"]) {
        [SVProgressHUD showErrorWithStatus:@"登录失败，请重试!" withDuration:3];
        return;
    }
    UserModel *user = [CommonCache getUserInfo];
    NSDictionary *params = @{@"code":code, @"uid":user.uid};
    [SVProgressHUD showWithStatus:@"正绑定中..."];
    [BoyeHttpClient post:@"BY_Weixin_bind" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        UserModel * user = [CommonCache getUserInfo];
        if ([result isKindOfClass:[NSString class]]) {
            user.wxopenid = (NSString *)result;
        }
        [CommonCache setUserInfo:user];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:2];
        UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        UILabel *label = (UILabel *)[cell viewWithTag:99999];
        label.text = @"已绑定";
       [SVProgressHUD showSuccessWithStatus:@"绑定成功" withDuration:1];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@绑定失败,请重试", error.domain] withDuration:3];
        
    }];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/





@end
